home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / editor / we16_95g.zip / WEEXTSRC.ZI_ / CTAGS.C next >
C/C++ Source or Header  |  1994-10-22  |  8KB  |  241 lines

  1. /*------------------------------------------------------------------------*
  2.  * CTAGS.C   - sample source for a GoToTag function.  To use this         *
  3.  *             function, add a command/menu mapping to WE_EXT.RC, add     *
  4.  *             a case statement for the new command, and compile and      *
  5.  *             link this file with WE_EXT.C.                              *
  6.  *                                                                        *
  7.  *             The TAGS.EXE program shipped with WinEdit generates a      *
  8.  *             tag file in the correct format with the following          *
  9.  *             command line:                                              *
  10.  *                                                                        *
  11.  *                 TAGS -om -tWINEDIT.MRK filespec                        *
  12.  *                                                                        *
  13.  *------------------------------------------------------------------------*/
  14.  
  15. #define STRICT
  16. #define _WINDLL
  17. #include <windows.h>
  18. #include "we_ext.h"
  19. #include "private.h"
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25.  
  26. char gszTemp[16];      /* this needs to be in DGROUP for atoi()  */
  27. char gszFileName[256]; /* this needs to be in DGROUP for fopen() */
  28.  
  29. #define BUFSIZE 16383
  30.  
  31. /*------------------------------------------------------------------------*
  32.  * GoToTag() - retrieves the word at the caret and looks up the           *
  33.  *             tag in WINEDIT.MRK.  If found, loads the appropriate       *
  34.  *             file and goes to that line.                                *
  35.  *                                                                        *
  36.  * RETURNS:    Nothing.                                                   *
  37.  *                                                                        *
  38.  *------------------------------------------------------------------------*/
  39. void GoToTag(HWND hWnd)
  40.    {
  41.    char szTemp[64];
  42.    char szLoadFile[256];
  43.    UINT uiLineNo;
  44.  
  45.    edEditGetCurrentWord(hWnd,szTemp,63);
  46.  
  47.    lstrcpy(gszFileName,"WINEDIT.MRK");
  48.    uiLineNo = FindToken(szTemp,szLoadFile);
  49.  
  50.    if (uiLineNo)
  51.       {
  52.       if (edFileOpen(hWnd,szLoadFile))
  53.          edEditGoToLine(hWnd,uiLineNo);
  54.       }
  55.    }
  56.  
  57.  
  58. /*------------------------------------------------------------------------*
  59.  *                                                                        *
  60.  * FindToken() - find the token lpToken in lpTagFileName.                 *
  61.  *                                                                        *
  62.  * RETURNS:     Line number if found.  0 if not.                          *
  63.  *                                                                        *
  64.  * The tag file must be in Microsoft format: TOKEN FILENAME(lineno).      *
  65.  *                                                                        *
  66.  * The TAGS.EXE program shipped with WinEdit generates a tag file         *
  67.  * in the correct format with the following command line:                 *
  68.  *                                                                        *
  69.  *          TAGS -om -tWINEDIT.MRK filespec                               *
  70.  *                                                                        *
  71.  *------------------------------------------------------------------------*/
  72. UINT FindToken(LPSTR szToken, LPSTR lpLoadFileName)
  73.    {
  74.    int f;
  75.    int i;
  76.    char szNewFile[256];
  77.    UINT uiLineNo,uiRval;
  78.    OFSTRUCT of;
  79.    HANDLE hBuffer;
  80.    LPSTR  lpBuffer;
  81.    LPSTR lpFoundIt;
  82.    LONG lFilePos;
  83.    
  84.    uiLineNo = 0;
  85.    f = OpenFile(gszFileName,&of,OF_READ|OF_SHARE_DENY_NONE);
  86.    if (f == HFILE_ERROR)
  87.       return 0;
  88. #ifdef WIN32
  89.    strupr(szToken);
  90. #else
  91.    _fstrupr(szToken);
  92. #endif   
  93.    hBuffer = GlobalAlloc(GHND,BUFSIZE+1);
  94.    if (!hBuffer)
  95.       return 0;
  96.       
  97.    lpBuffer = GlobalLock(hBuffer);   
  98.    
  99.    uiRval = _lread(f,lpBuffer,BUFSIZE);
  100.    
  101. #ifdef WIN32
  102.    strupr(lpBuffer);
  103. #else
  104.    _fstrupr(lpBuffer);
  105. #endif   
  106.  
  107.    /* back up to the last CR/LF */
  108.    if (uiRval)
  109.       i = uiRval-1;
  110.    else
  111.       i = 0;
  112.    while (i && (lpBuffer[i] != 0x0D))
  113.       i--;
  114.    if (i)
  115.       {
  116.       i += 2;
  117.       _llseek(f,i,0);
  118.       lFilePos = uiRval = (UINT)i;
  119.       }
  120.  
  121.    while (uiRval)
  122.       {
  123.       lpFoundIt = Scan(szToken,
  124.                        lpBuffer,
  125.                        uiRval);
  126.                        
  127.       if (lpFoundIt)
  128.          {
  129.          /* move to the file name */                                          
  130.          for (;*lpFoundIt != ' ';lpFoundIt++);                                
  131.  
  132.          lpFoundIt++;
  133.  
  134.          /* build the file name */                                            
  135.          for (i=0;(*lpFoundIt != ' ') && (*lpFoundIt != '(');i++,lpFoundIt++) 
  136.             szNewFile[i] = *lpFoundIt;                                        
  137.  
  138.          szNewFile[i] = 0;
  139.  
  140.          lpFoundIt++;                                                         
  141.  
  142.          /* grab the line number */
  143.          for (i=0;(*lpFoundIt != ' ') && (*lpFoundIt != ')');i++,lpFoundIt++)
  144.             gszTemp[i] = *lpFoundIt;
  145.  
  146.          gszTemp[i] = 0;
  147.  
  148.          uiLineNo = atoi(gszTemp);
  149.          break;
  150.          }
  151.       else
  152.          {
  153.          uiRval = _lread(f,lpBuffer,BUFSIZE);
  154.  
  155.          if (uiRval)
  156.             {
  157. #ifdef WIN32
  158.             strupr(lpBuffer);
  159. #else
  160.             _fstrupr(lpBuffer);
  161. #endif   
  162.  
  163.             /* back up to the last CR/LF */
  164.             i = uiRval-1;
  165.             while (i && (lpBuffer[i] != 0x0D))
  166.                i--;
  167.             if (i)
  168.                {
  169.                i += 2;
  170.                lFilePos += i;
  171.                _llseek(f,lFilePos,0);
  172.                uiRval = (UINT)i;
  173.                }
  174.             }
  175.          }
  176.       }
  177.  
  178.  
  179.    _lclose(f);
  180.  
  181.    lstrcpy(lpLoadFileName,szNewFile);
  182.  
  183.    return uiLineNo;
  184.    }
  185.    
  186.  
  187. /*------------------------------------------------------------------------*
  188.  * Scan()    - brute force buffer searcher.                               *
  189.  *                                                                        *
  190.  * RETURNS:    Pointer to matching string, NULL if not found.             *
  191.  *                                                                        *
  192.  *------------------------------------------------------------------------*/
  193.  
  194. LPSTR Scan(LPSTR lpPattern,
  195.              LPSTR lpText,
  196.              int   iLength)
  197.    {
  198.    LPSTR lpNext;
  199.    LPSTR lpFound;
  200.    LPSTR lpTemp;
  201.    LPSTR lpCheckText;
  202.    char cFirstChar;
  203.  
  204.    cFirstChar = lpPattern[0];
  205.    lpTemp = lpText;
  206.  
  207.    /* search for the first character */
  208. #ifdef WIN32
  209.    lpFound = lpNext = memchr(lpTemp,cFirstChar,iLength);
  210. #else
  211.    lpFound = lpNext = _fmemchr(lpTemp,cFirstChar,iLength);
  212. #endif
  213.    while (lpNext)
  214.       {
  215.       /* check for a complete match */
  216.       for (lpCheckText = lpPattern;
  217.            *lpCheckText && *lpNext && (*lpCheckText == *lpNext);
  218.            *lpCheckText++,*lpNext++);
  219.  
  220.       if (!(*lpCheckText))
  221.          /* we got a complete match */
  222.          return lpFound;
  223.  
  224.       iLength -= (lpNext - lpTemp);
  225.  
  226.       lpTemp = lpNext;
  227.  
  228.       /* search for the next occurence of the first character */
  229.       if (iLength > 0)
  230. #ifdef WIN32
  231.          lpFound = lpNext = memchr(lpTemp,cFirstChar,iLength);
  232. #else
  233.          lpFound = lpNext = _fmemchr(lpTemp,cFirstChar,iLength);
  234. #endif
  235.       else
  236.          break;
  237.       }
  238.  
  239.    return NULL;
  240.    }
  241.